package com.ruoyi.shop.emporium.order.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.mall.common.domain.DtsOrderStatus;
import com.ruoyi.mall.common.domain.DtsRefundzTrace;
import com.ruoyi.shop.emporium.order.service.IDtsOrderService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mall.common.domain.DtsOrder;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单管理Controller
 *
 * @author ruoyi
 * @date 2025-03-10
 */
@RestController
@RequestMapping("/emporium/order")
public class DtsOrderController extends BaseController
{
    @Autowired
    private IDtsOrderService dtsOrderService;

    /**
     * 查询订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('emporium:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(DtsOrder dtsOrder)
    {
        startPage();
        List<DtsOrder> list = dtsOrderService.selectDtsOrderList(dtsOrder);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('emporium:order:status')")
    @GetMapping("/status")
    public List<DtsOrderStatus> statusList() {
        return dtsOrderService.selectDtsOrderStatusList();
    }

    /**
     * 导出订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('emporium:order:export')")
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DtsOrder dtsOrder)
    {
        List<DtsOrder> list = dtsOrderService.selectDtsOrderList(dtsOrder);
        ExcelUtil<DtsOrder> util = new ExcelUtil<DtsOrder>(DtsOrder.class);
        util.exportExcel(response, list, "订单管理数据");
    }

    /**
     * 获取订单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('emporium:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dtsOrderService.selectDtsOrderById(id));
    }

    /**
     * 新增订单管理
     */
    @PreAuthorize("@ss.hasPermi('emporium:order:add')")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DtsOrder dtsOrder)
    {
        return toAjax(dtsOrderService.insertDtsOrder(dtsOrder));
    }

    /**
     * 修改订单管理
     */
    @PreAuthorize("@ss.hasPermi('emporium:order:edit')")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DtsOrder dtsOrder)
    {
        return toAjax(dtsOrderService.updateDtsOrder(dtsOrder));
    }

    /**
     * 删除订单管理
     */
    @PreAuthorize("@ss.hasPermi('emporium:order:remove')")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dtsOrderService.deleteDtsOrderByIds(ids));
    }


//    @PreAuthorize("@ss.hasPermi('emporium:order:refund')")
//    @GetMapping(value = "/refund")
//    public TableDataInfo getRefundInfo(DtsRefundzTrace dtsRefundzTrace) {
//        // 改为返回TableDataInfo结构
//        DtsRefundzTrace refund = dtsOrderService.selectRefund(dtsRefundzTrace);
//        return getDataTable(Arrays.asList(refund));
//    }

    @PreAuthorize("@ss.hasPermi('emporium:order:refund')")
    @GetMapping(value = "/refund/{orderSn}") // 添加/refund路径段
    public AjaxResult getRefundInfo(@PathVariable("orderSn") String orderSn) {
        return success(dtsOrderService.selectRefund(orderSn));
    }

    @PreAuthorize("@ss.hasPermi('emporium:order:refund')")
    @PostMapping("/refund/approve")
    public AjaxResult approveRefund(@RequestBody DtsRefundzTrace dtsRefundzTrace) {
        return success(dtsOrderService.approveRefund(dtsRefundzTrace));
    }

    @PreAuthorize("@ss.hasPermi('emporium:order:refund')")
    @PostMapping("/refund/complete")
    public AjaxResult completeRefund(@RequestBody DtsRefundzTrace dtsRefundzTrace) {
        return success(dtsOrderService.completeRefund(dtsRefundzTrace));
    }

}
